<script setup lang="ts">
import type { loginParams } from '@/types/user'
import { ref } from 'vue'
import { User, Lock } from '@element-plus/icons-vue'
import { useUserStore } from '@/store/modules/user'
import { useRoute, useRouter } from 'vue-router'
import { ElNotification, type FormInstance } from 'element-plus'

//表单对象
const loginForm = ref<loginParams>({
  username: import.meta.env.MODE === 'development' ? 'admin' : '',
  password: import.meta.env.MODE === 'development' ? '111111' : '',
})
//自定义表单规则
const validatorUserName = (_rule: any, value: any, callback: any) => {
  if (value.length >= 5) {
    callback()
  } else {
    callback(new Error('账号长度至少5位'))
  }
}
const validatorPassWord = (_rule: any, value: any, callback: any) => {
  if (value.length >= 6) {
    callback()
  } else {
    callback(new Error('密码至少6位'))
  }
}
//表单规则
const rules = ref({
  username: [
    {
      required: true,
      min: 4,
      max: 10,
      message: '用户名长度为4到10个字符',
      trigger: 'blur',
    },
    {
      trigger: 'blur',
      validator: validatorUserName,
    },
  ],
  password: [
    {
      required: true,
      min: 6,
      max: 15,
      message: '密码长度至少6位',
      trigger: 'blur',
    },
    {
      trigger: 'blur',
      validator: validatorPassWord,
    },
  ],
})
//获取仓库数据
const userStore = useUserStore()
//获取路由器
const router = useRouter()
//获取路由对象
const route = useRoute()
//按钮加载效果
const loading = ref(false)
// 获取表单实例
const loginFormRef = ref<FormInstance | null>(null)
//登录
const login = async () => {
  await loginFormRef.value!.validate()
  // 显示加载效果
  loading.value = true
  // 用户登录逻辑
  try {
    await userStore.userLogin(loginForm.value)
    // 关闭加载效果
    loading.value = false
    // 判断是否有路由参数,有跳转对应的路由,没有跳转到首页
    const redirect: any = route.query.redirect
    router.push({ path: redirect || '/' })

    ElNotification.success('登录成功')
  } catch (error) {
    //登录失败关闭加载效果
    loading.value = false
    // 登录失败提示
    ElNotification.error((error as Error).message)
  }
}
</script>

<template>
  <div class="login_container">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <!-- 表单组件 -->
        <el-form
          :model="loginForm"
          :rules="rules"
          class="login_form"
          ref="loginFormRef"
        >
          <!-- 标题 -->
          <h1>Hello</h1>
          <h2>欢迎来到电商管理系统</h2>
          <!-- 用户名表单项 -->
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              placeholder="请输入用户名"
              :prefix-icon="User"
            ></el-input>
          </el-form-item>

          <!-- 密码表单项 -->
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              placeholder="请输入密码"
              type="password"
              show-password
              :prefix-icon="Lock"
            ></el-input>
          </el-form-item>

          <!-- 登录按钮 -->
          <el-form-item>
            <el-button
              type="primary"
              class="login_btn"
              @click="login"
              :loading="loading"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<style lang="scss" scoped>
.login_container {
  width: 100%;
  height: 100vh;
  background: url('@/assets/images/background.jpg') no-repeat;
  background-size: cover;
  .login_form {
    position: relative;
    width: 80%;
    top: 30vh;
    background: url('@/assets/images/login_form.png') no-repeat;
    background-size: cover;
    padding: 40px;
    h1 {
      color: white;
      font-size: 40px;
    }
    h2 {
      font-size: 20px;
      color: white;
      margin: 20px 0px;
    }
    .login_btn {
      width: 100%;
    }
  }
}
</style>
